//
//  SettlementTableController.m
//  Futures
//
//  Created by Cheng on 2017/6/27.
//  Copyright © 2017年 Cheng. All rights reserved.
//



#import "SettlementTableController.h"
#import "TransacionViewModel.h"
#import "CloseOrderModel.h"
#import "SettlementCell.h"
#import "OrderDetailsViewController.h"
#define kSettlementCellID @"SettlementCellID"
#define kPageTitleViewH 36
#define kGetCloseHolder @"kGetCloseHolder"

@interface SettlementTableController () <UITableViewDelegate,UITableViewDataSource>{
    NSMutableArray *_closeArray;
}

@end

@implementation SettlementTableController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"订单记录";
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"back" highlightedIcon:nil target:self action:@selector(back)];
    
    if (!GLOBAL_INSTANCE.isLogin) {
        return;
    }
    
    [self refreshData];
    
    __weak typeof(self) weakSelf = self;
    [[JX_GCDTimerManager sharedInstance] scheduledDispatchTimerWithName:kGetCloseHolder timeInterval:5 queue:nil repeats:YES actionOption:AbandonPreviousAction action:^{
        [weakSelf refreshData];
    }];
}

- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
    self.tableView.contentInset = UIEdgeInsetsMake(-20, 0, -20, 0);
}

- (void)refreshData{
    [TransacionViewModel requestCloseOderWithBlck:^(id obj) {
        _closeArray = obj;
        [self.tableView reloadData];
    }];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return _closeArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    
//}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SettlementCell *cell = [tableView dequeueReusableCellWithIdentifier:kSettlementCellID forIndexPath:indexPath];
    
    [cell setCellWithModel:_closeArray[indexPath.section]];
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    CloseOrderModel *model = _closeArray[indexPath.section];
    
    [self performSegueWithIdentifier:@"OrderDetailsViewController" sender:model];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    OrderDetailsViewController *vc = segue.destinationViewController;
    vc.closeOrderModel = sender;
    NSLog(@"%@",sender);
}


@end
